home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1995 January / macformat-020.iso / Shareware City / Applications / Alpha.5.96 folder / Tcl / SystemCode / wordCompletion.tcl < prev   
Encoding:
Text File  |  1994-09-15  |  5.7 KB  |  184 lines  |  [TEXT/ALFA]

  1. #===========================================================================
  2. # 'Word Completion', in the spirit of Paul van Mulbregt's BBXT.
  3. # Composed by Mark Nagata (nagata@kurims.kyoto-u.ac.jp) 
  4. # for Alpha 5.76, 4/22/94.
  5. # Modified by Tim van der Leeuw (tnleeuw@cs.vu.nl), 9/14/94.
  6. # In the spirit of emacs.
  7. # I have modified this routine extensively to add the ability to complete
  8. # a word in multiple ways if called repeatedly.
  9. # For this purpose, I have introduced all the global variables (starting with
  10. # __wc__) and added the first if-statement -- everything between
  11. # 'set pos [getPos]' and 'backwardWord'. In the original routine, I've changed
  12. # some existing variables to globals, prefixing their name with '__wc__'.
  13. #
  14. # This code is probably not the most efficient tcl-code, nor the most elegant
  15. # tcl-code for this problem, but hey, it is the first function I've ever
  16. # written in tcl!
  17. # If anyone has some suggestions for improvement, or
  18. # knows of a better algorithm, I would like to know it.
  19. #================================================================================
  20. set __wc__insPos -1
  21. proc wordCompletion {} {
  22.     global __wc__len __wc__prevPos __wc__insPos __wc__prevFound __wc__pat __wc__nextStart __wc__fwd
  23.     
  24.     set pos [getPos]
  25.     if $pos==$__wc__insPos {
  26.         # Cursor changed place?
  27.         set skipStr $__wc__prevFound
  28.         while 1 {    
  29.             if $__wc__fwd {
  30.                 set fndMsg "found below."
  31.             } else {
  32.                 set fndMsg "found above."
  33.             }
  34.             if {![catch {search -f $__wc__fwd -r 1 -i 0 -m 1 -- $__wc__pat $__wc__nextStart} data]} {
  35.                 set d00 [lindex $data 0]
  36.                 set beg [expr $d00+$__wc__len]
  37.                 set end [lindex $data 1]
  38.                 set __wc__prevFound [getText $d00 $end]
  39.                 if [string compare $skipStr $__wc__prevFound] {
  40.                     # Have we got the same word twice?
  41.                     set txt [getText $beg $end]
  42.                     deleteText $__wc__prevPos $__wc__insPos
  43.                     goto $__wc__prevPos
  44.                     insertText $txt
  45.                     message $fndMsg
  46.                     # Set a number of globals for possible next go-around
  47.                     set __wc__insPos [getPos]
  48.                     if $__wc__fwd {
  49.                         # Search Forwards
  50.                         set __wc__nextStart $end
  51.                         # End of found word
  52.                     } else {
  53.                         # Search Backwards
  54.                         set __wc__nextStart [expr $d00 - $__wc__len]
  55.                         # Before start of found word
  56.                         if $__wc__nextStart<=0 {
  57.                             set __wc__fwd 1
  58.                             set __wc__nextStart $__wc__insPos
  59.                         }
  60.                     }
  61.                     return
  62.                 } else {
  63.                     # Move start of search after finding string again
  64.                     if $__wc__fwd {
  65.                         # Searching Forwards
  66.                         set __wc__nextStart $end
  67.                         # End of found word
  68.                     } else {
  69.                         # Still Searching Backwards
  70.                         set __wc__nextStart [expr $d00 - $__wc__len]
  71.                         # Before start of found word
  72.                         if $__wc__nextStart<=0 {
  73.                             set __wc__fwd 1
  74.                             set __wc__nextStart $__wc__insPos
  75.                         }
  76.                     }
  77.                 }
  78.                 # End if string compare 
  79.             } else {
  80.                 # Search string not found
  81.                 if $__wc__fwd {
  82.                     # We were already looking forward, so the word is not in the file
  83.                     message "Not found."
  84.                     set __wc__insPos -1
  85.                     goto $pos
  86.                     backwardWordSelect
  87.                     return
  88.                 } else {
  89.                     # start looking forward
  90.                     set __wc__fwd 1
  91.                     set __wc__nextStart $__wc__insPos
  92.                 }
  93.             }
  94.             
  95.         }
  96.     }
  97.     backwardWord
  98.     # Start new search for WordCompletion
  99.     set start [getPos]
  100.     set one [getText $start $pos]
  101.     set __wc__len [expr $pos-$start]
  102.     set __wc__pat [append one {[a-zA-Z0-9_]+}]
  103.     set start [expr $start-1]
  104.     if {![catch {search -f 0 -r 1 -i 0 -m 1 -- $__wc__pat $start} data]} {
  105.         set d00 [lindex $data 0]
  106.         set beg [expr $d00+$__wc__len]
  107.         set end [lindex $data 1]
  108.         set __wc__prevFound [getText $d00 $end ]
  109.         set txt [getText $beg $end]
  110.         goto $pos
  111.         insertText $txt
  112.         message "found above."
  113.         # Set a number of globals for possible next go-around
  114.         set __wc__insPos [getPos]
  115.         set __wc__prevPos $pos
  116.         set __wc__nextStart [expr $d00-$__wc__len]
  117.         set __wc__fwd 0
  118.         return
  119.     }
  120.     if {![catch {search -f 1 -r 1 -i 0 -m 1 -- $__wc__pat $pos} data]} {
  121.         set __wc__prevFound [getText [lindex $data 0] [lindex $data 1] ]
  122.         set beg [expr [lindex $data 0]+$__wc__len]
  123.         set end [lindex $data 1]
  124.         set txt [getText $beg $end]
  125.         goto $pos
  126.         insertText $txt
  127.         message "found below."
  128.         # Set a number of globals for possible next go-around
  129.         set __wc__insPos [getPos]
  130.         set __wc__prevPos $pos
  131.         set __wc__nextStart $end
  132.         set __wc__fwd 1
  133.         return
  134.     }
  135.     goto $pos
  136.     backwardWordSelect
  137. }
  138.  
  139.  
  140.  
  141. # This is all due to the idea of Paul van Mulbregt. In his documentation 
  142. # of his BBEdit BBExtension (info-mac/text/bbedit-fl-package-11.hqx), 
  143. # he explains:
  144. # --  From the documentation written by        --
  145. # --  Paul van Mulbregt (paulvm@dragonsys.com) --
  146. # Word Completion
  147. # This extension saves typing, as well as making sure variable names are 
  148. # correct.  While typing the following C code, there is no need to type all 
  149. # of the second occurrence of verySpecialInt.  One could copy and paste, but 
  150. # another way is to type a few letters, and select the Word Completion 
  151. # Extension.
  152. #     int verySpecialInt = 10;
  153. #     while(verySp                                                    
  154. # becomes
  155. #     int verySpecialInt = 10;
  156. #     while(verySpecialInt                                             
  157. #                                                     
  158. # Word Completion will look back in the code to find the first match and then 
  159. # extend the current occurrence to match the previous occurrence.  If a match 
  160. # is not found looking backwards, then it looks forwards.  If not found 
  161. # forwards, the word is selected.  This is a quick way to save on typing, 
  162. # good for helping prevent RSI, but perhaps more importantly, to make sure 
  163. # that variable names are spelt correctly.
  164. # There is no user interface for Word Completion.
  165. # The usefulness of this extension is greatly enhanced if the extension is 
  166. # bound to a key!
  167. # -- end of Paul van Mulbregt's explanation. --
  168.  
  169.  
  170.